<!DOCTYPE html>
<html lang="zh-CN">
<head>
  <meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=2">
<meta name="theme-color" content="#222">
<meta name="generator" content="Hexo 5.2.0">


  <link rel="apple-touch-icon" sizes="180x180" href="/murmur/images/apple-touch-icon-next.png">
  <link rel="icon" type="image/png" sizes="32x32" href="/murmur/images/favicon-32x32-next.png">
  <link rel="icon" type="image/png" sizes="16x16" href="/murmur/images/favicon-16x16-next.png">
  <link rel="mask-icon" href="/murmur/images/logo.svg" color="#222">

<link rel="stylesheet" href="/murmur/css/main.css">



<link rel="stylesheet" href="//cdn.jsdelivr.net/npm/@fortawesome/fontawesome-free@5.15.1/css/all.min.css">
  <link rel="stylesheet" href="//cdn.jsdelivr.net/npm/animate.css@3.1.1/animate.min.css">
  <link rel="stylesheet" href="//cdn.jsdelivr.net/npm/nprogress@0.2.0/nprogress.css">
  <script src="//cdn.jsdelivr.net/npm/nprogress@0.2.0/nprogress.js"></script>

<script class="hexo-configurations">
    var NexT = window.NexT || {};
    var CONFIG = {"hostname":"gitee.com","root":"/murmur/","scheme":"Gemini","version":"8.0.2","exturl":false,"sidebar":{"position":"left","display":"post","padding":18,"offset":12},"copycode":true,"bookmark":{"enable":false,"color":"#222","save":"auto"},"fancybox":false,"mediumzoom":false,"lazyload":false,"pangu":false,"comments":{"style":"tabs","active":"disqus","storage":true,"lazyload":false,"nav":null},"motion":{"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"fadeInDown","post_body":"fadeInDown","coll_header":"fadeInLeft","sidebar":"fadeInUp"}},"prism":false,"i18n":{"placeholder":"搜索...","empty":"没有找到任何搜索结果：${query}","hits_time":"找到 ${hits} 个搜索结果（用时 ${time} 毫秒）","hits":"找到 ${hits} 个搜索结果"}};
  </script>

  <meta name="description" content="ES6的Symbol怎么用">
<meta property="og:type" content="article">
<meta property="og:title" content="ES6 Symbol的特性与思考">
<meta property="og:url" content="https://gitee.com/lianghuilin/murmur.git/ecma_script/ES6%20Symbol%E7%9A%84%E7%89%B9%E6%80%A7%E4%B8%8E%E6%80%9D%E8%80%83/index.html">
<meta property="og:site_name" content="汇话板">
<meta property="og:description" content="ES6的Symbol怎么用">
<meta property="og:locale" content="zh_CN">
<meta property="article:published_time" content="2021-05-02T06:29:12.000Z">
<meta property="article:modified_time" content="2021-11-28T11:00:51.641Z">
<meta property="article:author" content="William Leung">
<meta name="twitter:card" content="summary">


<link rel="canonical" href="https://gitee.com/lianghuilin/murmur.git/ecma_script/ES6%20Symbol%E7%9A%84%E7%89%B9%E6%80%A7%E4%B8%8E%E6%80%9D%E8%80%83/">


<script class="page-configurations">
  // https://hexo.io/docs/variables.html
  CONFIG.page = {
    sidebar: "",
    isHome : false,
    isPost : true,
    lang   : 'zh-CN'
  };
</script>

  <title>ES6 Symbol的特性与思考 | 汇话板</title>
  


  <script>
    var _hmt = _hmt || [];
    (function() {
      var hm = document.createElement("script");
      hm.src = "https://hm.baidu.com/hm.js?a49081c570a782a242cdf6e19e1a584d";
      var s = document.getElementsByTagName("script")[0];
      s.parentNode.insertBefore(hm, s);
    })();
  </script>




  <noscript>
  <style>
  body { margin-top: 2rem; }

  .use-motion .menu-item,
  .use-motion .sidebar,
  .use-motion .post-block,
  .use-motion .pagination,
  .use-motion .comments,
  .use-motion .post-header,
  .use-motion .post-body,
  .use-motion .collection-header {
    visibility: visible;
  }

  .use-motion .header,
  .use-motion .site-brand-container .toggle,
  .use-motion .footer { opacity: initial; }

  .use-motion .site-title,
  .use-motion .site-subtitle,
  .use-motion .custom-logo-image {
    opacity: initial;
    top: initial;
  }

  .use-motion .logo-line {
    transform: scaleX(1);
  }

  .search-pop-overlay, .sidebar-nav { display: none; }
  .sidebar-panel { display: block; }
  </style>
</noscript>

</head>

<body itemscope itemtype="http://schema.org/WebPage" class="use-motion">
  <div class="headband"></div>

  <main class="main">
    <header class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-container">
  <div class="site-nav-toggle">
    <div class="toggle" aria-label="切换导航栏">
        <span class="toggle-line"></span>
        <span class="toggle-line"></span>
        <span class="toggle-line"></span>
    </div>
  </div>

  <div class="site-meta">

    <a href="/murmur/" class="brand" rel="start">
      <i class="logo-line"></i>
      <h1 class="site-title">汇话板</h1>
      <i class="logo-line"></i>
    </a>
      <p class="site-subtitle" itemprop="description">设定目标，放慢步调，用温和的力量自我改革</p>
  </div>

  <div class="site-nav-right">
    <div class="toggle popup-trigger">
    </div>
  </div>
</div>



<nav class="site-nav">
  <ul class="main-menu menu">
        <li class="menu-item menu-item-home">

    <a href="/murmur/" rel="section"><i class="fa fa-home fa-fw"></i>首页</a>

  </li>
        <li class="menu-item menu-item-categories">

    <a href="/murmur/categories/" rel="section"><i class="fa fa-th fa-fw"></i>分类</a>

  </li>
  </ul>
</nav>




</div>
        
  
  <div class="toggle sidebar-toggle">
    <span class="toggle-line"></span>
    <span class="toggle-line"></span>
    <span class="toggle-line"></span>
  </div>

  <aside class="sidebar">

    <div class="sidebar-inner sidebar-nav-active sidebar-toc-active">
      <ul class="sidebar-nav">
        <li class="sidebar-nav-toc">
          文章目录
        </li>
        <li class="sidebar-nav-overview">
          站点概览
        </li>
      </ul>

      <div class="sidebar-panel-container">
        <!--noindex-->
        <section class="post-toc-wrap sidebar-panel">
            <div class="post-toc animated"><ol class="nav"><li class="nav-item nav-level-2"><a class="nav-link" href="#%E5%85%88%E7%AE%80%E5%8D%95%E8%AF%B4%E8%AF%B4%E4%BB%80%E4%B9%88%E6%98%AFSymbol"><span class="nav-number">1.</span> <span class="nav-text">先简单说说什么是Symbol</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#Symbol%E7%9A%84%E7%89%B9%E6%80%A7%E4%B8%8E%E4%BD%BF%E7%94%A8"><span class="nav-number">2.</span> <span class="nav-text">Symbol的特性与使用</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#%E7%A4%BA%E4%BE%8B%EF%BC%9A%E5%88%9B%E5%BB%BASymbol%E6%95%B0%E6%8D%AE"><span class="nav-number">2.1.</span> <span class="nav-text">示例：创建Symbol数据</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E7%89%B9%E6%80%A7%EF%BC%9ASymbol%E6%80%BB%E6%98%AF%E5%94%AF%E4%B8%80%E7%9A%84"><span class="nav-number">2.2.</span> <span class="nav-text">特性：Symbol总是唯一的</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E7%A4%BA%E4%BE%8B%EF%BC%9A%E4%BD%BF%E7%94%A8Symbol%E6%9D%A5%E5%AE%9A%E4%B9%89%E5%B8%B8%E9%87%8F"><span class="nav-number">2.3.</span> <span class="nav-text">示例：使用Symbol来定义常量</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E7%A4%BA%E4%BE%8B%EF%BC%9A%E4%BD%BF%E7%94%A8Symbol%E6%9D%A5%E5%AE%9A%E4%B9%89%E4%BA%BA%E5%90%8D"><span class="nav-number">2.4.</span> <span class="nav-text">示例：使用Symbol来定义人名</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E7%89%B9%E6%80%A7%EF%BC%9ASymbol%E7%9A%84%E7%B1%BB%E5%9E%8B%E5%88%A4%E6%96%AD%E5%92%8C%E7%B1%BB%E5%9E%8B%E8%BD%AC%E6%8D%A2"><span class="nav-number">2.5.</span> <span class="nav-text">特性：Symbol的类型判断和类型转换</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E7%89%B9%E6%80%A7%EF%BC%9ASymbol%E5%8F%AF%E4%BD%9C%E4%B8%BA%E5%AF%B9%E8%B1%A1%E7%9A%84%E5%B1%9E%E6%80%A7key%E5%90%8D"><span class="nav-number">2.6.</span> <span class="nav-text">特性：Symbol可作为对象的属性key名</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E7%A4%BA%E4%BE%8B%EF%BC%9A%E9%80%9A%E8%BF%87Symbol%E6%A8%A1%E6%8B%9F%E5%AF%B9%E8%B1%A1%E7%9A%84%E7%A7%81%E6%9C%89%E5%B1%9E%E6%80%A7%E6%88%96%E8%80%85%E7%A7%81%E6%9C%89%E6%96%B9%E6%B3%95"><span class="nav-number">2.7.</span> <span class="nav-text">示例：通过Symbol模拟对象的私有属性或者私有方法</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E7%A4%BA%E4%BE%8B%EF%BC%9A%E5%88%A9%E7%94%A8Symbol%E8%BF%9B%E8%A1%8C%E6%95%B0%E6%8D%AE%E5%BD%92%E9%9B%86%E5%92%8C%E6%95%B4%E5%90%88"><span class="nav-number">2.8.</span> <span class="nav-text">示例：利用Symbol进行数据归集和整合</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E7%B3%BB%E7%BB%9FSymbol"><span class="nav-number">2.9.</span> <span class="nav-text">系统Symbol</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E5%8F%82%E8%80%83"><span class="nav-number">3.</span> <span class="nav-text">参考</span></a></li></ol></div>
        </section>
        <!--/noindex-->

        <section class="site-overview-wrap sidebar-panel">
          <div class="site-author site-overview-item animated" itemprop="author" itemscope itemtype="http://schema.org/Person">
  <p class="site-author-name" itemprop="name">William Leung</p>
  <div class="site-description" itemprop="description"></div>
</div>
<div class="site-state-wrap site-overview-item animated">
  <nav class="site-state">
      <div class="site-state-item site-state-posts">
          <a href="/murmur/archives">
          <span class="site-state-item-count">43</span>
          <span class="site-state-item-name">文章</span>
        </a>
      </div>
      <div class="site-state-item site-state-categories">
            <a href="/murmur/categories/">
          
        <span class="site-state-item-count">9</span>
        <span class="site-state-item-name">分类</span></a>
      </div>
      <div class="site-state-item site-state-tags">
            <a href="/murmur/tags/">
        <span class="site-state-item-count">1</span>
        <span class="site-state-item-name">标签</span></a>
      </div>
  </nav>
</div>



        </section>
      </div>
    </div>
  </aside>
  <div class="sidebar-dimmer"></div>


    </header>

    
  <div class="back-to-top">
    <i class="fa fa-arrow-up"></i>
    <span>0%</span>
  </div>

<noscript>
  <div class="noscript-warning">Theme NexT works best with JavaScript enabled</div>
</noscript>


    <div class="main-inner post posts-expand">
      

      

  


<div class="post-block">
  
  

  <article itemscope itemtype="http://schema.org/Article" class="post-content" lang="zh-CN">
    <link itemprop="mainEntityOfPage" href="https://gitee.com/lianghuilin/murmur.git/ecma_script/ES6%20Symbol%E7%9A%84%E7%89%B9%E6%80%A7%E4%B8%8E%E6%80%9D%E8%80%83/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/murmur/images/avatar.gif">
      <meta itemprop="name" content="William Leung">
      <meta itemprop="description" content="">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="汇话板">
    </span>
      <header class="post-header">
        <h1 class="post-title" itemprop="name headline">
          ES6 Symbol的特性与思考
        </h1>

        <div class="post-meta-container">
          <div class="post-meta">
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-calendar"></i>
      </span>
      <span class="post-meta-item-text">发表于</span>

      <time title="创建时间：2021-05-02 14:29:12" itemprop="dateCreated datePublished" datetime="2021-05-02T14:29:12+08:00">2021-05-02</time>
    </span>
      <span class="post-meta-item">
        <span class="post-meta-item-icon">
          <i class="far fa-calendar-check"></i>
        </span>
        <span class="post-meta-item-text">更新于</span>
        <time title="修改时间：2021-11-28 19:00:51" itemprop="dateModified" datetime="2021-11-28T19:00:51+08:00">2021-11-28</time>
      </span>
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-folder"></i>
      </span>
      <span class="post-meta-item-text">分类于</span>
        <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
          <a href="/murmur/categories/ECMAScript%E6%89%8B%E6%9C%AD/" itemprop="url" rel="index"><span itemprop="name">ECMAScript手札</span></a>
        </span>
    </span>

  
    <span class="post-meta-item" title="本文字数">
      <span class="post-meta-item-icon">
        <i class="far fa-file-word"></i>
      </span>
      <span class="post-meta-item-text">本文字数：</span>
      <span>6.5k</span>
    </span>
    <span class="post-meta-item" title="阅读时长">
      <span class="post-meta-item-icon">
        <i class="far fa-clock"></i>
      </span>
      <span class="post-meta-item-text">阅读时长 &asymp;</span>
      <span>6 分钟</span>
    </span>
</div>

            <div class="post-description">ES6的Symbol怎么用</div>
        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">
        <h2 id="先简单说说什么是Symbol"><a href="#先简单说说什么是Symbol" class="headerlink" title="先简单说说什么是Symbol"></a>先简单说说什么是Symbol</h2><ul>
<li>Symbol是ES6新增的基础数据类型，它的特点就是独一无二的，如同UUID一样；</li>
<li>Symbol是函数，通过调用Symbol函数来创建Symbol数据；</li>
<li>Symbol还是内置对象，提供一系列函数well-known Symbol方法来改变JS语言的内部行为；</li>
</ul>
<h2 id="Symbol的特性与使用"><a href="#Symbol的特性与使用" class="headerlink" title="Symbol的特性与使用"></a>Symbol的特性与使用</h2><h3 id="示例：创建Symbol数据"><a href="#示例：创建Symbol数据" class="headerlink" title="示例：创建Symbol数据"></a>示例：创建Symbol数据</h3><ul>
<li>Symbol没有字面量的创建方式，也不能以<code>new Symbol()</code>构造函数的方式创建，只能通过调用<code>Symbol([description])</code>函数，或者<code>Symbol.for()</code>来创建。</li>
</ul>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line"><span class="comment">// Symbol不允许以new关键的构造函数方式调用</span></span><br><span class="line"><span class="keyword">new</span> <span class="built_in">Symbol</span>()</span><br><span class="line"><span class="comment">// Uncaught TypeError: Symbol is not a constructor</span></span><br><span class="line"></span><br><span class="line"><span class="comment">// 创建无描述的Symbol数据</span></span><br><span class="line"><span class="keyword">let</span> symbol1 = <span class="built_in">Symbol</span>()</span><br><span class="line"></span><br><span class="line"><span class="comment">// 创建带描述的Symbol数据</span></span><br><span class="line"><span class="keyword">let</span> localSymbol <span class="built_in">Symbol</span>(<span class="string">&#x27;desc1&#x27;</span>)</span><br><span class="line"></span><br><span class="line"><span class="comment">// 在全局环境创建Symbol数据</span></span><br><span class="line"><span class="keyword">let</span> globalSymbol = <span class="built_in">Symbol</span>.for(<span class="string">&#x27;desc1&#x27;</span>)</span><br><span class="line"></span><br><span class="line"><span class="comment">// 全局注册表的Symbol和Symbol函数创建的Symbol是不一样的</span></span><br><span class="line"><span class="built_in">console</span>.log(localSymbol === globalSymbol)</span><br><span class="line"><span class="comment">// 输出：false</span></span><br><span class="line"></span><br></pre></td></tr></table></figure>

<h3 id="特性：Symbol总是唯一的"><a href="#特性：Symbol总是唯一的" class="headerlink" title="特性：Symbol总是唯一的"></a>特性：Symbol总是唯一的</h3><ul>
<li>数据总是独一无二的，不仅在函数、模块甚至在window顶层作用域中都是唯一的</li>
</ul>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 调用Symbol函数，在当前环境中（函数作用域/模块作用域）创建唯一的symbol数据，虽然toString输出的结果看起来一样，但两者是不相等的</span></span><br><span class="line"><span class="keyword">let</span> f1 = <span class="built_in">Symbol</span>(<span class="string">&#x27;flag&#x27;</span>)</span><br><span class="line"><span class="keyword">let</span> f2 = <span class="built_in">Symbol</span>(<span class="string">&#x27;flag&#x27;</span>)</span><br><span class="line"></span><br><span class="line"><span class="built_in">console</span>.log(f1,f2)</span><br><span class="line"><span class="comment">// 输出：Symbol(flag) Symbol(flag)</span></span><br><span class="line"></span><br><span class="line"><span class="built_in">console</span>.log(f1 === f2)</span><br><span class="line"><span class="comment">// 输出：false</span></span><br><span class="line"><span class="built_in">console</span>.log(f1 === <span class="string">&#x27;flag&#x27;</span>)</span><br><span class="line"><span class="comment">// 输出false</span></span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="comment">// 调用Symbol.for(key)方法，在是全局环境中创建Symbol数据。</span></span><br><span class="line"><span class="comment">// 当调用方法是，会根据key名来进行幂等操作，发现不存在则创建，如果已存在则返回  </span></span><br><span class="line"><span class="keyword">let</span> lock = <span class="built_in">Symbol</span>.for(<span class="string">&#x27;flag&#x27;</span>)</span><br><span class="line"><span class="keyword">let</span> lockFlag = <span class="built_in">Symbol</span>.for(<span class="string">&#x27;flag&#x27;</span>)</span><br><span class="line"><span class="built_in">console</span>.log(lock === lockFlag)</span><br><span class="line"><span class="comment">// 输出：true</span></span><br><span class="line"></span><br><span class="line"><span class="comment">// Symbol()函数和Symbol.for()创建的Symbol数据不一样</span></span><br><span class="line"><span class="built_in">console</span>.log(f1 === lock)</span><br><span class="line"><span class="comment">// 输出：false</span></span><br><span class="line"></span><br><span class="line"><span class="comment">// 在全局环境下如果不想创建，只想查找，可通过Symbol.keyFor()方法</span></span><br><span class="line"><span class="built_in">console</span>.log(<span class="built_in">Symbol</span>.keyFor(<span class="string">&#x27;flag&#x27;</span>)) <span class="comment">// flag</span></span><br><span class="line"><span class="built_in">console</span>.log(<span class="built_in">Symbol</span>.keyFor(<span class="string">&#x27;test&#x27;</span>)) <span class="comment">// undefined但不会创建</span></span><br></pre></td></tr></table></figure>

<h3 id="示例：使用Symbol来定义常量"><a href="#示例：使用Symbol来定义常量" class="headerlink" title="示例：使用Symbol来定义常量"></a>示例：使用Symbol来定义常量</h3><ul>
<li>既然Symbol的特性是唯一标志，我们可以用Symbol来做常量。</li>
<li>以前我们定义常量是这样婶的：</li>
</ul>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> FRUIT =  &#123;</span><br><span class="line">  APPLE: <span class="string">&#x27;APPLE&#x27;</span>,</span><br><span class="line">  BANANA: <span class="string">&#x27;BANANA&#x27;</span>,</span><br><span class="line">  STRAWBERRY: <span class="string">&#x27;STRAWBERRY&#x27;</span>  </span><br><span class="line">&#125; </span><br><span class="line"></span><br><span class="line"><span class="comment">// 调用的时候，其实我们并不关心value是什么，只看key</span></span><br><span class="line"><span class="built_in">console</span>.log(FRUIT.APPLE)</span><br><span class="line"></span><br><span class="line"><span class="comment">//但万一有个傻子，加了一个菠萝，但值写成了苹果，判断就会炸裂</span></span><br><span class="line"><span class="keyword">const</span> FRUIT =  &#123;</span><br><span class="line">  APPLE: <span class="string">&#x27;APPLE&#x27;</span>,</span><br><span class="line">  BANANA: <span class="string">&#x27;BANANA&#x27;</span>,</span><br><span class="line">  STRAWBERRY: <span class="string">&#x27;STRAWBERRY&#x27;</span>,</span><br><span class="line">  PINEAPPLE: <span class="string">&#x27;APPLE&#x27;</span>  <span class="comment">// 新增</span></span><br><span class="line">&#125; </span><br><span class="line"></span><br></pre></td></tr></table></figure>

<ul>
<li>通过Symbol定义的话，就会避免这样的问题：</li>
</ul>
<iframe width="100%" height="300" src="//jsrun.net/BP3Kp/embedded/js,result/light" allowfullscreen="allowfullscreen" frameborder="0"></iframe>

<h3 id="示例：使用Symbol来定义人名"><a href="#示例：使用Symbol来定义人名" class="headerlink" title="示例：使用Symbol来定义人名"></a>示例：使用Symbol来定义人名</h3><ul>
<li>比如一个班级里面，想通过人名来作为唯一标识，但人名又没办法避免重复，通过Symbol来实现</li>
</ul>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> grade = &#123;</span><br><span class="line">  [<span class="built_in">Symbol</span>(<span class="string">&#x27;Lily&#x27;</span>)]: &#123;</span><br><span class="line">    address: <span class="string">&#x27;shenzhen&#x27;</span>,</span><br><span class="line">    tel: <span class="string">&#x27;186******78&#x27;</span></span><br><span class="line">  &#125;,</span><br><span class="line">  [<span class="built_in">Symbol</span>(<span class="string">&#x27;Annie&#x27;</span>)]: &#123;</span><br><span class="line">    address: <span class="string">&#x27;guangzhou&#x27;</span>,</span><br><span class="line">    tel: <span class="string">&#x27;183******12&#x27;</span></span><br><span class="line">  &#125;,</span><br><span class="line">  <span class="comment">// 允许重复的名称</span></span><br><span class="line">  [<span class="built_in">Symbol</span>(<span class="string">&#x27;Lily&#x27;</span>)]: &#123;</span><br><span class="line">    address: <span class="string">&#x27;beijing&#x27;</span>,</span><br><span class="line">    tel: <span class="string">&#x27;172******10&#x27;</span></span><br><span class="line">  &#125;,</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h3 id="特性：Symbol的类型判断和类型转换"><a href="#特性：Symbol的类型判断和类型转换" class="headerlink" title="特性：Symbol的类型判断和类型转换"></a>特性：Symbol的类型判断和类型转换</h3><ul>
<li>和String类型一样，Symbol类型可以通过<code>typeof</code>操作符进行类型判断</li>
</ul>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">let</span> symbol = <span class="built_in">Symbol</span>()</span><br><span class="line"><span class="built_in">console</span>.log(<span class="keyword">typeof</span> <span class="built_in">Symbol</span>)</span><br><span class="line"><span class="comment">// 输出：symbol</span></span><br></pre></td></tr></table></figure>

<ul>
<li>但和String类型不一样的是，Symbol不会进行隐式的自动类型转换，所以不能直接进行字符串拼接运算和算术运算。但可以人为的进行显式类型转换，比如转成String、Boolean、Number、Object</li>
</ul>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">let</span> symbolUUID = <span class="built_in">Symbol</span>(<span class="string">&#x27;uuid&#x27;</span>)</span><br><span class="line"></span><br><span class="line"><span class="comment">// 不能直接进行字符串拼接操作</span></span><br><span class="line"><span class="built_in">console</span>.log(symbolUUID + <span class="string">&#x27;测试&#x27;</span>) </span><br><span class="line"><span class="comment">// TypeError: Cannot convert a Symbol value to a string</span></span><br><span class="line"></span><br><span class="line"><span class="comment">// 也不能直接进行算数操作</span></span><br><span class="line"><span class="built_in">console</span>.log(symbolUUID + <span class="number">1</span>) </span><br><span class="line"><span class="comment">// TypeError: Cannot convert a Symbol value to a number</span></span><br><span class="line"></span><br><span class="line"><span class="comment">// 但可以进行三目运算的boolean判断操作</span></span><br><span class="line"><span class="built_in">console</span>.log(symbolUUID ? <span class="string">&#x27;真&#x27;</span> : <span class="string">&#x27;假&#x27;</span>)</span><br><span class="line"><span class="comment">// 输出：真</span></span><br><span class="line"></span><br><span class="line"><span class="built_in">console</span>.log(<span class="built_in">String</span>(symbolUUID) + <span class="string">&#x27;测试&#x27;</span>)</span><br><span class="line"><span class="comment">// 输出：Symbol(uuid)测试</span></span><br><span class="line"><span class="comment">// 等价于symbolUUID.toString()</span></span><br><span class="line"></span><br><span class="line"></span><br></pre></td></tr></table></figure>

<h3 id="特性：Symbol可作为对象的属性key名"><a href="#特性：Symbol可作为对象的属性key名" class="headerlink" title="特性：Symbol可作为对象的属性key名"></a>特性：Symbol可作为对象的属性key名</h3><ul>
<li>根据规范，Symbol类型可以作为数据单独存在，也可以作为对象的属性key名。并且，对象的属性key只能是字符串类型或者Symbol类型，没有别的数据类型可以作为属性key，Boolean不行，Number也不行。</li>
<li>但值得注意的是，需要以<code>&#123;[SymbolKey]: value&#125;</code>数组括弧的方式来挂载。</li>
</ul>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 作为对象的属性名</span></span><br><span class="line"><span class="keyword">let</span> desc = <span class="built_in">Symbol</span>(<span class="string">&#x27;desc&#x27;</span>)</span><br><span class="line"><span class="keyword">let</span> person = &#123;</span><br><span class="line">  name: <span class="string">&#x27;huilin&#x27;</span>,</span><br><span class="line">  sex: <span class="string">&#x27;男&#x27;</span>,</span><br><span class="line">  [desc]: <span class="string">&#x27;职位：前端工程师&#x27;</span></span><br><span class="line">&#125;</span><br><span class="line"><span class="comment">// 或者可以这样赋值：person[desc] = &#x27;职位：前端工程师&#x27;</span></span><br><span class="line"><span class="built_in">console</span>.log(person)</span><br><span class="line"><span class="comment">// 输出：&#123;name: &#x27;huilin&#x27;,sex: &#x27;男&#x27;,Symbol(&#x27;desc&#x27;): &#x27;职位：前工程师&#x27;&#125;</span></span><br><span class="line"></span><br></pre></td></tr></table></figure>

<ul>
<li>Symbol作为属性名时具有弱隐藏性</li>
</ul>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/*</span></span><br><span class="line"><span class="comment"> * 常规的方式获取对象属性，会自动忽略Symbol属性的键值对的</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="comment">// 上面的例子如果进行JSON.stringify()格式化操作，会忽略Symbol</span></span><br><span class="line"><span class="built_in">console</span>.log(<span class="built_in">JSON</span>.stringify(person))</span><br><span class="line"><span class="comment">// 输出：&#123;name: &#x27;huilin&#x27;,sex: &#x27;男&#x27;&#125;  </span></span><br><span class="line"></span><br><span class="line"><span class="comment">// 同样的，像for循环这样的常规遍历操作，会忽略Symbol</span></span><br><span class="line"><span class="keyword">for</span>(key <span class="keyword">in</span> person)&#123;</span><br><span class="line">    <span class="built_in">console</span>.log(key)</span><br><span class="line">&#125;</span><br><span class="line"><span class="comment">// 输出： name sex</span></span><br><span class="line"></span><br><span class="line"><span class="comment">// Object.keys()会忽略Symbol</span></span><br><span class="line"><span class="built_in">console</span>.log(<span class="built_in">Object</span>.keys(person))</span><br><span class="line"><span class="comment">// 输出： [ &#x27;name&#x27;, &#x27;sex&#x27; ]</span></span><br><span class="line"></span><br><span class="line"><span class="comment">// Object.getProperty()忽略Symbol</span></span><br><span class="line"><span class="built_in">console</span>.log(<span class="built_in">Object</span>.getOwnPropertyNames(person))</span><br><span class="line"><span class="comment">// 输出：[ &#x27;name&#x27;, &#x27;sex&#x27; ]</span></span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="comment">/*</span></span><br><span class="line"><span class="comment"> * 仅获取Symbol属性的键值对的方法</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="built_in">console</span>.log(<span class="built_in">Object</span>.getOwnPropertySymbol(person))</span><br><span class="line"><span class="comment">// 输出：[ Symbol(desc) ]</span></span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="comment">/*</span></span><br><span class="line"><span class="comment"> * 同时获取常规属性和Symbol属性的方法</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"></span><br><span class="line"><span class="built_in">console</span>.log(<span class="built_in">Reflect</span>.ownKeys(person))</span><br><span class="line"><span class="comment">// 输出：[ &#x27;name&#x27;, &#x27;sex&#x27;, Symbol(desc) ]</span></span><br></pre></td></tr></table></figure>

<h3 id="示例：通过Symbol模拟对象的私有属性或者私有方法"><a href="#示例：通过Symbol模拟对象的私有属性或者私有方法" class="headerlink" title="示例：通过Symbol模拟对象的私有属性或者私有方法"></a>示例：通过Symbol模拟对象的私有属性或者私有方法</h3><ul>
<li>借助Symbol属性名的弱隐藏性，模拟私有属性</li>
</ul>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// Symbol类型的属性名</span></span><br><span class="line"><span class="keyword">const</span> id = <span class="built_in">Symbol</span>()</span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">User</span> </span>&#123;</span><br><span class="line">  <span class="function"><span class="title">constructor</span>(<span class="params">idVal, name, age</span>)</span>&#123;</span><br><span class="line">    <span class="built_in">this</span>[id] = idVal</span><br><span class="line">    <span class="built_in">this</span>.name = name</span><br><span class="line">    <span class="built_in">this</span>.age = age</span><br><span class="line">  &#125;</span><br><span class="line"></span><br><span class="line">  <span class="function"><span class="title">checkId</span>(<span class="params">id</span>)</span>&#123;</span><br><span class="line">    <span class="keyword">return</span> <span class="built_in">this</span>[id] === id</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">// 私有属性，外部实例不能直接获取</span></span><br><span class="line"><span class="keyword">let</span> u = <span class="keyword">new</span> User(<span class="string">&#x27;001&#x27;</span>, <span class="string">&#x27;Jay&#x27;</span>, <span class="number">40</span>)</span><br><span class="line"><span class="built_in">console</span>.log(u.name, u.age, u[id])</span><br><span class="line"><span class="comment">// 输出：Jay 40 001</span></span><br><span class="line"></span><br><span class="line"><span class="comment">// 但是通过对外暴露的方法，能访问到私有属性</span></span><br><span class="line"><span class="built_in">console</span>.log(u.checkId(<span class="string">&#x27;001&#x27;</span>)) <span class="comment">// true</span></span><br><span class="line"><span class="built_in">console</span>.log(u.checkId(<span class="string">&#x27;002&#x27;</span>)) <span class="comment">// false</span></span><br><span class="line"></span><br><span class="line"></span><br></pre></td></tr></table></figure>

<h3 id="示例：利用Symbol进行数据归集和整合"><a href="#示例：利用Symbol进行数据归集和整合" class="headerlink" title="示例：利用Symbol进行数据归集和整合"></a>示例：利用Symbol进行数据归集和整合</h3><ul>
<li>拿张鑫旭大佬打听小美眉的例子来看，通常情况下两个对象合并，key相同则会覆盖：</li>
</ul>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">let</span> info1 = &#123;</span><br><span class="line">  name: <span class="string">&#x27;小雪&#x27;</span>,</span><br><span class="line">  age: <span class="number">24</span>,</span><br><span class="line">  job: <span class="string">&#x27;前端工程师&#x27;</span>,</span><br><span class="line">  desc: <span class="string">&#x27;喜欢看电影，已经有交往对象&#x27;</span></span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">let</span> info2 = &#123;</span><br><span class="line">  desc: <span class="string">&#x27;喜欢小狗，住在南山区，上下班坐公交车&#x27;</span></span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">// 由于使用desc是String作为key，key相同会覆盖</span></span><br><span class="line"><span class="built_in">console</span>.log(<span class="built_in">Object</span>.assgin(info1,info2)) </span><br><span class="line"><span class="comment">// 输出：&#123;name: &#x27;小雪&#x27;,age: 24,job: &#x27;前端工程师&#x27;,desc: &#x27;喜欢小狗，住在南山区，上下班坐公交车&#x27;&#125;</span></span><br></pre></td></tr></table></figure>

<ul>
<li>那改成用Symbol作为属性key名会怎样呢？Symbol不会进行覆盖的</li>
</ul>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">let</span> info1 = &#123;</span><br><span class="line">  name: <span class="string">&#x27;小雪&#x27;</span>,</span><br><span class="line">  age: <span class="number">24</span>,</span><br><span class="line">  job: <span class="string">&#x27;前端工程师&#x27;</span>,</span><br><span class="line">  [<span class="built_in">Symbol</span>(<span class="string">&#x27;desc&#x27;</span>)]: <span class="string">&#x27;喜欢看电影，已经有交往对象&#x27;</span></span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">let</span> info2 = &#123;</span><br><span class="line">  [<span class="built_in">Symbol</span>(<span class="string">&#x27;desc&#x27;</span>)]: <span class="string">&#x27;喜欢小狗，住在南山区，上下班坐公交车&#x27;</span></span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">// 通过Symbol作为key，合并会保留</span></span><br><span class="line"><span class="built_in">console</span>.log(<span class="built_in">Object</span>.assgin(info1,info2)) </span><br><span class="line"><span class="comment">// 输出：&#123;name: &#x27;小雪&#x27;,age: 24,job: &#x27;前端工程师&#x27;,Symbol(&#x27;desc&#x27;): &#x27;喜欢看电影，已经有交往对象&#x27;, Symbol(&#x27;desc&#x27;): &#x27;喜欢小狗，住在南山区，上下班坐公交车&#x27;&#125;</span></span><br></pre></td></tr></table></figure>

<ul>
<li>可见，Symbol更关注的是value值，而不是key名。可以思考得出，Symbol的特性就是方便对数据进行归集和整合。</li>
<li>拿现实中的例子来说吧，微信文章的点赞墙，数据值都是点赞，但记录不会被覆盖，用户的头像都会罗列出来；再比如签到簿，数据值是时间，很有可能是扎堆签到时间一样，但也不会被覆盖，而是把记录罗列进来。</li>
<li>再回到JavaScript语法层面，可能大家会觉得，名字冲突这种事情，概率很低吧？有必要专门新增一个Symbol嘛？但是你想啊，ES6的Module，导入导出是可以起别名的；还有ES6的解构，可以直接获取对象的属性名到当前环境；这样你还觉得名字冲突的概率低吗？</li>
<li>所以Symbol通过归集和整合的特性，针对基础框架版本升级时，便于同名的方法或者变量向下兼容。</li>
</ul>
<h3 id="系统Symbol"><a href="#系统Symbol" class="headerlink" title="系统Symbol"></a>系统Symbol</h3><ul>
<li>除了自己创建Symbol标记之外，ES6还提供了一系列内置的well-know(众所周知)的Symbol标记，用于改变JavaScript底层API的行为</li>
</ul>
<table>
<thead>
<tr>
<th>API</th>
<th>desc</th>
</tr>
</thead>
<tbody><tr>
<td>Symbol.hasInstance</td>
<td>当调用instanceof运算符判断实例时，会调用这个方法</td>
</tr>
<tr>
<td>Symbol.isConcatSpreadable</td>
<td>当调用Array.prototype.concat()时，判断是否展开</td>
</tr>
<tr>
<td>Symbol.unscopables</td>
<td>对象指定使用with关键字时，哪些属性会被with环境排除</td>
</tr>
<tr>
<td>Symbol.match</td>
<td>当执行str.match(obj)时，如果该属性存在会调用它，并返回方法的返回值</td>
</tr>
<tr>
<td>Symbol.replace</td>
<td>当执行str.replace(obj)时调用，并返回方法的返回值</td>
</tr>
<tr>
<td>Symbol.search</td>
<td>当执行str.search(obj)时调用，并返回方法的返回值</td>
</tr>
<tr>
<td>Symbol.split</td>
<td>当执行str.split(obj)时调用，并返回方法的返回值</td>
</tr>
<tr>
<td>Symbol.iterator</td>
<td>当对象进行for…of循环时，调用Symbol.iterator方法，返回该对象默认遍历器</td>
</tr>
<tr>
<td>Symbol.toPrimitive</td>
<td>当对象被转换为原始数据类型时调用，返回该对象对应的原始数据类型</td>
</tr>
<tr>
<td>Symbol.toStringTag</td>
<td>在该对象调用toString方法时调用，返回方法的返回值</td>
</tr>
<tr>
<td>Symbol.species</td>
<td>创建衍生对象时使用该属性</td>
</tr>
</tbody></table>
<h2 id="参考"><a href="#参考" class="headerlink" title="参考"></a>参考</h2><ul>
<li><a target="_blank" rel="noopener" href="https://www.zhangxinxu.com/wordpress/2018/04/known-es6-symbol-function/">张鑫旭-简单了解ES6/ES2015 Symbol() 方法</a></li>
<li><a target="_blank" rel="noopener" href="https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/Symbol">MDN-Symbol对象</a></li>
<li><a target="_blank" rel="noopener" href="https://zh.javascript.info/symbol">JavaScript.info-Symbol类型</a></li>
<li><a target="_blank" rel="noopener" href="https://blog.fundebug.com/2019/03/19/why-does-javascript-need-symbol/">FunDebug-JavaScript 为什么要有Symbol类型</a></li>
<li><a target="_blank" rel="noopener" href="https://www.infoq.cn/article/es6-in-depth-symbols">infoQ-深入浅出 ES6 Symbol</a></li>
<li><a target="_blank" rel="noopener" href="https://blog.csdn.net/qq_42205731/article/details/107631375">陨石猎人-理解和使用ES6中的Symbol</a></li>
</ul>

    </div>

    
    
    

    <footer class="post-footer">
          

<div class="post-copyright">
<ul>
  <li class="post-copyright-author">
      <strong>本文作者： </strong>William Leung
  </li>
  <li class="post-copyright-link">
      <strong>本文链接：</strong>
      <a href="https://gitee.com/lianghuilin/murmur.git/ecma_script/ES6%20Symbol%E7%9A%84%E7%89%B9%E6%80%A7%E4%B8%8E%E6%80%9D%E8%80%83/" title="ES6 Symbol的特性与思考">https://gitee.com/lianghuilin/murmur.git/ecma_script/ES6 Symbol的特性与思考/</a>
  </li>
  <li class="post-copyright-license">
    <strong>版权声明： </strong>本博客所有文章除特别声明外，均采用 <a href="https://creativecommons.org/licenses/by-nc-sa/4.0/" rel="noopener" target="_blank"><i class="fab fa-fw fa-creative-commons"></i>BY-NC-SA</a> 许可协议。转载请注明出处！
  </li>
</ul>
</div>


        

          <div class="post-nav">
            <div class="post-nav-item">
                <a href="/murmur/vue3_practice/%E5%9B%BE%E8%A7%A3vue-cli%E7%8E%AF%E5%A2%83%E5%8F%98%E9%87%8F%E5%92%8C%E6%A8%A1%E5%BC%8F/" rel="prev" title="图解vue-cli环境变量和模式">
                  <i class="fa fa-chevron-left"></i> 图解vue-cli环境变量和模式
                </a>
            </div>
            <div class="post-nav-item">
                <a href="/murmur/vue3_practice/Vue3%E4%BD%BF%E7%94%A8mitt%E8%BF%9B%E8%A1%8C%E7%BB%84%E4%BB%B6%E9%80%9A%E4%BF%A1/" rel="next" title="Vue3使用mitt进行组件通信">
                  Vue3使用mitt进行组件通信 <i class="fa fa-chevron-right"></i>
                </a>
            </div>
          </div>
    </footer>
  </article>
</div>






      

<script>
  window.addEventListener('tabs:register', () => {
    let { activeClass } = CONFIG.comments;
    if (CONFIG.comments.storage) {
      activeClass = localStorage.getItem('comments_active') || activeClass;
    }
    if (activeClass) {
      const activeTab = document.querySelector(`a[href="#comment-${activeClass}"]`);
      if (activeTab) {
        activeTab.click();
      }
    }
  });
  if (CONFIG.comments.storage) {
    window.addEventListener('tabs:click', event => {
      if (!event.target.matches('.tabs-comment .tab-content .tab-pane')) return;
      const commentClass = event.target.classList[1];
      localStorage.setItem('comments_active', commentClass);
    });
  }
</script>

    </div>
  </main>

  <footer class="footer">
    <div class="footer-inner">
      

      

<div class="copyright">
  
  &copy; 
  <span itemprop="copyrightYear">2023</span>
  <span class="with-love">
    <i class="fa fa-heart"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">William Leung</span>
</div>
<div class="wordcount">
  <span class="post-meta-item">
    <span class="post-meta-item-icon">
      <i class="fa fa-chart-line"></i>
    </span>
    <span title="站点总字数">142k</span>
  </span>
  <span class="post-meta-item">
    <span class="post-meta-item-icon">
      <i class="fa fa-coffee"></i>
    </span>
    <span title="站点阅读时长">2:09</span>
  </span>
</div>
<div class="busuanzi-count">
    <span id="busuanzi_container_site_uv">
      本站访客数<span id="busuanzi_value_site_uv"></span>人次
    </span>
    <span id="busuanzi_container_site_pv">
        本站总访问量<span id="busuanzi_value_site_pv"></span>次
    </span>
</div>
  <div class="powered-by">由 <a href="https://hexo.io/" class="theme-link" rel="noopener" target="_blank">Hexo</a> & <a href="https://theme-next.js.org/" class="theme-link" rel="noopener" target="_blank">NexT.Gemini</a> 强力驱动
  </div>

    </div>
  </footer>

  
  <script src="//cdn.jsdelivr.net/npm/animejs@3.2.1/lib/anime.min.js"></script>
<script src="/murmur/js/utils.js"></script><script src="/murmur/js/motion.js"></script><script src="/murmur/js/next-boot.js"></script>

  
















  <script>
    NProgress.configure({
      showSpinner: true
    });
    NProgress.start();
    document.addEventListener('readystatechange', () => {
      if (document.readyState === 'interactive') {
        NProgress.inc(0.8);
      }
      if (document.readyState === 'complete') {
        NProgress.done();
      }
    });
    document.addEventListener('pjax:send', () => {
      NProgress.start();
    });
    document.addEventListener('pjax:success', () => {
      NProgress.done();
    });
  </script>


  
  <script async src="https://busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script>








  

  

</body>
</html>
